// subPackage/view/user/login.js
let app = getApp();
import Notify from '@vant/weapp/notify/notify';
let that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logining: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    that = this;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  continueView() {
    let switchTabs = [
      "/pages/index/index", "/pages/centre/index", "/pages/cardsercet/index"
    ]
    let curUrl = wx.getStorageSync('currentPath');
    let method = "navigateTo";
    if (!curUrl) {
      curUrl = "/pages/index/index";
    }
    if (curUrl.charAt(0) != '/') {
      curUrl = '/' + curUrl;
    }
    if (switchTabs.indexOf(curUrl) > -1) {
      method = "switchTab"
    }

    wx[method]({
      url: curUrl,
      success: function (e) {
        var page = getCurrentPages().pop();
        if (page == undefined || page == null) return;
        page.onLoad();
      }
    })
  },
  login() {
    this.setData({
      logining: true
    })
    wx.login({
      success(res) {
        app.request("user/login", "POST", {
          code: res.code
        }, 'xform').then(res => {
          let userinfo = res.data;
          wx.setStorageSync('userinfo', userinfo);
          that.setData({
            logining: false
          });

          Notify({
            type: 'success',
            message: '登录成功',
            selector: '#notify_selector',
            onClose(){
              that.continueView()
            }
          })

        }).catch(e => {
          that.setData({
            logining: false
          })
          Notify({
            type: 'danger',
            message: e.msg,
            selector: '#notify_selector',
          })
        })
      }
    })
  }
})
